package com.yugao.fintech.draper.generate.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yugao.fintech.draper.core.model.ResultVO;
import com.yugao.fintech.draper.datasource.entity.PageResult;
import com.yugao.fintech.draper.generate.entity.DynamicDatasource;
import com.yugao.fintech.draper.generate.service.impl.DynamicDatasourceServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据源表
 */
@RestController
@RequestMapping("/datasourceConf")
@RequiredArgsConstructor
public class DynamicDatasourceController {

	private final DynamicDatasourceServiceImpl datasourceConfService;

	/**
	 * 动态添加数据源
	 * @param datasourceConf 数据源信息
	 */
	@PostMapping("/addDs")
	public ResultVO<Boolean> addDs(@RequestBody DynamicDatasource datasourceConf) {
		return ResultVO.success(datasourceConfService.addDs(datasourceConf));
	}

	/**
	 * 删除数据源信息
	 * @param id 数据源主键
	 */
	@DeleteMapping("/delete")
	public ResultVO<Boolean> delete(@RequestParam("id") Long id) {
		return ResultVO.success(datasourceConfService.delete(id));
	}

	/**
	 * 修改数据源信息
	 * @param datasourceConf 数据源信息
	 * @return ture Or false
	 */
	@PostMapping("/update")
	public ResultVO<Boolean> updateDsConf(@RequestBody DynamicDatasource datasourceConf) {
		return ResultVO.success(datasourceConfService.updateDsConf(datasourceConf));
	}

	/**
	 * 根据id查询数据源信息
	 * @param id 数据源id
	 * @return 数据源信息
	 */
	@GetMapping("/findById")
	public ResultVO<DynamicDatasource> findById(@RequestParam("id") Long id) {
		return ResultVO.success(datasourceConfService.findBtId(id));
	}

	/**
	 * 分页查询数据源管理信息
	 * @param page 分页参数
	 * @return 分页数据
	 */
	@GetMapping(value = "/page")
	public ResultVO<PageResult<DynamicDatasource>> page(Page<DynamicDatasource> page) {
		Page<DynamicDatasource> result = datasourceConfService.pageDataSourceConf(page, Wrappers.emptyWrapper());
		return ResultVO.success(PageResult.success(result));
	}

	/**
	 * 查询所有数据源信息
	 */
	@GetMapping("/listDs")
	public ResultVO<List<DynamicDatasource>> listDs() {
		return ResultVO.success(datasourceConfService.listDs());
	}

}